<?php

/**
 * 
 */
class Admin_AuthController extends App_Controller_Default
{
	/**
	 * 
	 * @var Admin_Model_Mapper_Auth
	 */
	protected $_mapper;

	/**
	 * 
	 * (non-PHPdoc)
	 * @see Zend_Controller_Action::init()
	 */
	public function init ()
	{
		$this->_mapper = new Admin_Model_Mapper_Auth();
		
		$this->_helper->layout()->disableLayout();
		$this->_helper->viewRenderer->setRender('index');
	}
	
	/**
	 * 
	 * @access protected
	 * @return Admin_Form_Auth
	 */
	protected function _getFormAuth ()
	{
		$form = new Admin_Form_Auth();
		$form->setAction( $this->_helper->url('login') );
		
		return $form;
	}
	
	/**
	 * 
	 * @access protected
	 * @return Admin_Form_Recovery
	 */
	protected function _getFormRecovery ()
	{
		$form = new Admin_Form_Recovery();
		$form->setAction( $this->_helper->url('recovery') );
		
		return $form;
	}

	/**
	 * 
	 * @access public
	 * @return void
	 */
	public function indexAction ()
	{
		$this->view->formAuth 		= $this->_getFormAuth(); 
		$this->view->formRecovery 	= $this->_getFormRecovery(); 
	}

	/**
	 * 
	 * @access public 
	 * @return void
	 */
	public function loginAction ()
	{
		if ( Zend_Auth::getInstance()->hasIdentity() || !$this->getRequest()->isXmlHttpRequest() ) {
			$this->_helper->redirector->goToSimple('index', 'index');
			return;
		}
		
		$session = new Zend_Session_Namespace( Zend_Registry::get('config')->geral->appid );
		
		$route = empty($session->triedroute) ? $this->_helper->url('index', 'index') : $session->triedroute;
		
		$session->triedroute = null;
		unset($session->triedroute);
		
		$result = array('redirect' => $route, 'valid' => false);
		
		if ( $this->getRequest()->isPost() ) {
			
			$this->_mapper->setData( $this->getRequest()->getPost() );
			
			$result['valid'] = $this->_mapper->login();
			
		}
		
		$this->_helper->json($result);
	}

	/**
	 * 
	 * @access public
	 * @return void
	 */
	public function logoutAction ()
	{
		$auth = Zend_Auth::getInstance();
		
		$auth->getStorage()->clear();
		$auth->clearIdentity();
		
		//Nome do cookie
		$name = 'auth_admin_' . Zend_Registry::get('config')->geral->appid;
		
		setcookie( $name, false, time(), '/' );
		
		$this->_helper->redirector->gotoSimple('index');
	}
	
	/**
	 * 
	 * @access public
	 * @return void
	 */
	public function recoveryAction ()
	{
		$result = array('valid' => false);
		
		if ( $this->getRequest()->isPost() ) {
			
			$mapper = new Model_Mapper_Auth();
			
			$mapper->setData( $this->getRequest()->getPost() );
			
			$result['valid'] = $mapper->recovery();
			
		}
		
		$this->_helper->json( $result );
	} 

	/**
	 * 
	 * @access public
	 * @return void
	 */
	public function passwordAction ()
	{			
		$hash = $this->_getParam('hash', null);
			
		if ( empty($hash) ) 
			$this->_helper->redirector->gotoSimple( 'index' );
			
		$mapper = new Model_Mapper_Auth();
			
		$mapper->setData( array('hash' => $hash) );
		
		$this->view->result = $mapper->password();
		
		$this->_helper->viewRenderer->setRender('password');
	}
}
